package com.example.appointment.activity;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.example.appointment.R;
import com.example.appointment.common.http.HttpCallbackListener;
import com.example.appointment.common.http.HttpUtil;
import com.example.appointment.common.http.RequestUtil;
import com.readystatesoftware.systembartint.SystemBarTintManager;

import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;


/**
 *
 */
public class BaseActivity extends AppCompatActivity {
    private SystemBarTintManager tintManager;
    protected Context mContext;
    private int mView;
    protected RequestUtil requestUtil;
    private int colorres = R.color.white;//导航栏的颜色(默认为白色)
    @Nullable
    @Bind(R.id.main_title)
    TextView titile;
    @Nullable
    @Bind(R.id.main_save)
    TextView savebtn;
    /**
     * 容器
     */
    private ViewGroup container;

    @Nullable
    public TextView getSavebtn() {
        savebtn.setVisibility(View.VISIBLE);
        return savebtn;
    }

    /**
     * 设置颜色
     * notification
     *
     * @param colorres
     */
    public void setnotificationColore(int colorres) {
        this.colorres = colorres;
        initWindow();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        mContext = this;
        super.setContentView(R.layout.activity_base);
        container = (ViewGroup) findViewById(R.id.container);
        requestUtil = new RequestUtil(this);
        //eventbus 实例        // 界面创建时，订阅事件， 接受消息
        EventBus.getDefault().register(this);
    }


    @Override
    public void setContentView(int layoutResID) {
        initWindow();
        if (container != null) {
            container.addView(View.inflate(mContext, layoutResID, null));
        } else {
            super.setContentView(layoutResID);
        }

        ButterKnife.bind(this);
    }


    @Override
    public void setContentView(View view) {
        if (container != null) {
            container.addView(view);
        } else {
            super.setContentView(view);
        }
        ButterKnife.bind(this);
    }

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        if (container != null) {
            container.addView(view, params);
        } else {
            super.setContentView(view);
        }
        ButterKnife.bind(this);
    }

    public void postWithProgress(String url, Map<String, Object> params,
                                 final HttpCallbackListener listener) {
        requestUtil.requestWithProgress(url, HttpUtil.RequestMethod.POST, params, listener);
    }

    public void postWithoutProgress(String url, Map<String, Object> params,
                                    final HttpCallbackListener listener) {
        requestUtil.requestWithoutProgress(url, HttpUtil.RequestMethod.POST, params, listener);
    }

    public void getWithoutProgress(String url, Map<String, Object> params,
                                   final HttpCallbackListener listener) {
        requestUtil.requestWithoutProgress(url, HttpUtil.RequestMethod.GET, params, listener);
    }

    public void getWithProgress(String url, Map<String, Object> params,
                                final HttpCallbackListener listener) {
        requestUtil.requestWithProgress(url, HttpUtil.RequestMethod.GET, params, listener);
    }

    public void putWithoutProgress(String url, Map<String, Object> params,
                                   final HttpCallbackListener listener) {
        requestUtil.requestWithoutProgress(url, HttpUtil.RequestMethod.PUT, params, listener);
    }

    public void putWithProgress(String url, Map<String, Object> params,
                                final HttpCallbackListener listener) {
        requestUtil.requestWithoutProgress(url, HttpUtil.RequestMethod.PUT, params, listener);
    }


    public void deleteWithoutProgress(String url, Map<String, Object> params,
                                      final HttpCallbackListener listener) {
        requestUtil.requestWithoutProgress(url, HttpUtil.RequestMethod.DELETE, params, listener);
    }

    public void deleteWithProgress(String url, Map<String, Object> params,
                                   final HttpCallbackListener listener) {
        requestUtil.requestWithoutProgress(url, HttpUtil.RequestMethod.DELETE, params, listener);
    }

    @Override
    protected void onDestroy() {
        //ImageLoader.getInstance().clearMemoryCache();
        // 界面销毁时，取消订阅
        EventBus.getDefault().unregister(this);
        System.gc();
        super.onDestroy();
    }


//    @Override
//    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        FragmentManager fm = getSupportFragmentManager();
//        int index = requestCode >> 16;
//        if (index != 0) {
//            index--;
//            if (fm.getFragments() == null || index < 0
//                    || index >= fm.getFragments().size()) {
////				Log.w(TAG, "Activity result fragment index out of range: 0x"
////						+ Integer.toHexString(requestCode));
//                return;
//            }
//            Fragment frag = fm.getFragments().get(index);
//            if (frag == null) {
////				Log.w(TAG, "Activity result no fragment exists for index: 0x"
////						+ Integer.toHexString(requestCode));
//            } else {
//                handleResult(frag, requestCode, resultCode, data);
//            }
//            return;
//        }
//
//    }
//


    /**
     * 按返回键退出到後台運行
     *
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            moveTaskToBack(false);
//            return true;
            return super.onKeyDown(keyCode, event);
        }
        return super.onKeyDown(keyCode, event);
    }

    private void initWindow() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //状态栏透明
            setTranslucentStatus(true);
//            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //导航栏（虚拟按键透明）
//            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            tintManager = new SystemBarTintManager(this);

            tintManager.setStatusBarTintResource(colorres);
            //0xffE61A5F
            tintManager.setStatusBarTintEnabled(true);
        }
    }


    @TargetApi(19)
    private void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }

    /**
     * 返回按钮
     */
    @Nullable
    @OnClick(R.id.backbtn)
    public void backbtn() {
        this.finish();
    }


    protected void setTitile(String title) {
        titile.setText(title);
    }
    /**
     * 与发布者在同一个线程
     *
     * @param msg 事件1
     */
    public void onEvent(Object msg) {
    }

    /**
     * 执行在主线程。
     * 非常实用，可以在这里将子线程加载到的数据直接设置到界面中。
     *
     * @param msg 事件1
     */
    public void onEventMainThread(Object msg) {
    }

    /**
     * 执行在子线程，如果发布者是子线程则直接执行，如果发布者不是子线程，则创建一个再执行
     * 此处可能会有线程阻塞问题。
     *
     * @param msg 事件1
     */
    public void onEventBackgroundThread(Object msg) {
    }

    /**
     * 执行在在一个新的子线程
     * 适用于多个线程任务处理， 内部有线程池管理。
     *
     * @param msg 事件1
     */
    public void onEventAsync(Object msg) {
    }

}
